package com.api.gbhg.luoao.web;


import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.dao.DataBaseUtil;
import com.api.gbhg.liuwenbo.pojo.Info;
import com.api.gbhg.liuwenbo.utils.AlgorithmUtil;

import com.api.gbhg.liuwenbo.utils.FormatUtil;
import com.api.gbhg.luoao.utils.PortalUtils;
import com.api.gbhg.luoao.utils.Upperpart;

import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.api.gbhg.zengping.page.PageInfo;
import weaver.conn.RecordSet;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Path("/Project")
public class ProjectImplementation {
    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();

    /**
     * 获取项目执行情况统计数据
     * @return
     */
    @POST
    @Path("/condition")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getcondition(Map<String,Object> maps){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

        Integer pageNum = 1;
        Integer pageSize = 5;
        String nd = sdf.format(new Date());
        String dw = "2";
        if(maps.containsKey("pageNum") && maps.get("pageNum") instanceof Integer &&(Integer)maps.get("pageNum")>0)
            pageNum = (Integer) maps.get("pageNum");
        if(maps.containsKey("pageSize") && maps.get("pageSize") instanceof Integer && (Integer)maps.get("pageSize")>0)
            pageSize = (Integer) maps.get("pageSize");
        if(maps.containsKey("nd") && maps.get("nd") instanceof Integer )
            nd = maps.get("nd").toString();
        if(maps.containsKey("dw") && maps.get("dw") instanceof Integer){
            dw =  AlgorithmUtil.getSubCompanIdBybmId((Integer) maps.get("dw"));
        }

        RecordSet rs = PortalUtils.getPjStRs(pageNum,pageSize,nd,dw);
        Map<String,Object>[] map = new HashMap[rs.getArray().size()];
        int i = 0;
        while (rs.next()){
            map[i] = new HashMap<String,Object>();
            map[i].put("xmmc",rs.getString("xmmc"));
            RecordSet sbdwRs = rmcd.getSBDWById(rs.getInt("sbdw"));
            sbdwRs.next();
            map[i].put("ssdw",sbdwRs.getString("SUBCOMPANYNAME"));
            map[i].put("ysje",rs.getString("ze"));
            map[i].put("zxl",AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"),rs.getDouble("ze")));
            i++;
        }
        PageInfo<String> pageInfo=new PageInfo<>();
        int cut = PortalUtils.getPjStCount(nd,dw);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(cut);
        pageInfo.setList(map);
        return JSON.toJSONString(pageInfo);
    }

    /**
     * 下属单位执行率
     * @param maps
     * @return
     */
    @POST
    @Path("/SubordinateUnits")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getSubordinateUnits(Map<String,Object> maps){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Integer pageNum = 1;
        Integer pageSize = 5;
        String nd = sdf.format(new Date());
        String dw = "2";
        if(maps.containsKey("pageNum") && maps.get("pageNum") instanceof Integer &&(Integer)maps.get("pageNum")>0)
            pageNum = (Integer) maps.get("pageNum");
        if(maps.containsKey("pageSize") && maps.get("pageSize") instanceof Integer && (Integer)maps.get("pageSize")>0)
            pageSize = (Integer) maps.get("pageSize");
        if(maps.containsKey("nd") && maps.get("nd") instanceof Integer)
            nd = maps.get("nd").toString();
        if(maps.containsKey("dw") && maps.get("dw") instanceof Integer){
            dw =  PortalUtils.getSubCompanIdBybmId((Integer) maps.get("dw"));
        }

        RecordSet rs = PortalUtils.getSubordinate(pageNum,pageSize,nd,dw);
        Map<String,Object>[] map = new HashMap[rs.getArray().size()];
        int i = 0;
        while (rs.next()){
            map[i] = new HashMap<String,Object>();
            RecordSet sbdwRs = rmcd.getSBDWById(rs.getInt("sbdw"));
            sbdwRs.next();
            map[i].put("ssdw",sbdwRs.getString("SUBCOMPANYNAME"));
            map[i].put("zxl",AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"),rs.getDouble("ze")));
            map[i].put("sjzxl","0.00");
            i++;
        }
        PageInfo<String> pageInfo=new PageInfo<>();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        RecordSet getconnt = PortalUtils.getconnt(nd, dw);
        pageInfo.setTotal(getconnt.getArray().size());
        pageInfo.setList(map);
        return JSON.toJSONString(pageInfo);

    }



    /**
     * 获取 执行率、拨款、支付等数据
     * @return
     */
    @POST
    @Path("/upperpart")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getData(Map<String, Object> param) {
        Upperpart  upp = new Upperpart(param);
        upp.setImplementRate();//预算执行率
        upp.setCenterAppia();//中央财政拨款
        upp.setlocalAppia();//地方财政拨款
        upp.setCenterTransfer();//中央转移支付

        return JSON.toJSONString(upp.getPaIf());
    }


    /**
     * 预算流程
     * @param param
     * @return
     */
    @POST
    @Path("/getBudgetProcess")
    @Produces(MediaType.APPLICATION_JSON)
    public String getBudgetProcess(Map<String,Object> param){

        int nd = Integer.parseInt(param.get("nd").toString());

        String dw = "2";
        if(param.containsKey("dw") && param.get("dw") instanceof Integer){
            dw =  AlgorithmUtil.getSubCompanIdBybmId((Integer) param.get("dw"));

        }

        Info[] budgetPs = new Info[2];
        //预算流程
        Map<String, Integer> cut = PortalUtils.getApprovalTask(nd,dw);
        Map<String,Object>[] map = new HashMap[1];
        budgetPs[0] = new Info();
        map[0] = new HashMap<>();
        map[0].put("ycl",cut.get("processed"));
        map[0].put("wcl",cut.get("unprocessed"));
        budgetPs[0].setTitle("核批任务");
        budgetPs[0].setData(map);
        budgetPs[0].setId("hprw");

        //预算执行
        RecordSet bdilRs = PortalUtils.getBudgetImplementation(nd,dw);
        budgetPs[1] = new Info();
        Map<String,Object>[]  bdMap = new HashMap[1];
        bdMap[0] = new HashMap<>();
        if((bdilRs.getDouble("ze") >=0 )){
            bdMap[0].put("yzlje", FormatUtil.DmFormat((bdilRs.getDouble("ze") - bdilRs.getDouble("syed"))));
            bdMap[0].put("yzlbl",AlgorithmUtil.getRealTimeImplementRate(bdilRs.getDouble("syed"),bdilRs.getDouble("ze")));
        }else{
            bdMap[0].put("yzlje",  FormatUtil.DmFormat(0));
            bdMap[0].put("yzlbl",AlgorithmUtil.getRealTimeImplementRate(0,0));
        }
        budgetPs[1].setTitle("预算执行");
        budgetPs[1].setData(bdMap);
        budgetPs[1].setId("yszx");


        return JSON.toJSONString(budgetPs);
    }



    /**
     * 预算核批
     * @param param
     * @return
     */
    @POST
    @Path("/getBudgetApproval")
    @Produces(MediaType.APPLICATION_JSON)
    public String getBudgetApproval(Map<String,Object> param){

        Integer pageNum = 1;
        Integer pageSize = 5;
        boolean state = true;
        if(param.containsKey("pageNum") && param.get("pageNum") instanceof Integer &&(Integer)param.get("pageNum")>0)
            pageNum = (Integer) param.get("pageNum");
        if(param.containsKey("pageSize") && param.get("pageSize") instanceof Integer && (Integer)param.get("pageSize")>0)
            pageSize = (Integer) param.get("pageSize");
        if(param.containsKey("state") && param.get("state") instanceof Boolean )
            state = (boolean) param.get("state");
        int nd =Integer.parseInt(param.get("nd").toString());
        String dw = "2";
        if(param.containsKey("dw") && param.get("dw") instanceof Integer){
            dw =  AlgorithmUtil.getSubCompanIdBybmId((Integer) param.get("dw"));
        }
        RecordSet data = PortalUtils.getBudAppData(pageNum, pageSize,nd, dw,state);
        Map<String,Object>[] map = new HashMap[data.getArray().size()];
        int i = 0;
        while (data.next()){
            map[i] = new HashMap<String,Object>();
            map[i].put("xmmc",data.getString("xmmin"));
            map[i].put("yszje",data.getString("yszje"));
            map[i].put("cjsj",data.getString("cjsj"));
            i++;
        }
        PageInfo<String> pageInfo=new PageInfo<>();
        int cut = PortalUtils.getBudAppCount(nd,dw,state);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(cut);
        pageInfo.setList(map);
        return JSON.toJSONString(pageInfo);
    }

}
